﻿using System;
using System.Xml.Linq;

namespace FlightToolkit.SimProp
{
    internal enum Localize
    {
        Unknown,
        No,
        Yes
    }

    internal enum Cardinality
    {
        Unknown,
        Single,
        Multiple
    }

    internal class SymbolDef
    {
        internal Guid Id { get; private set; }
        internal string Name { get; private set; }
        public TypeDef Type { get; protected set; }
        internal NamespaceDef namespaceDef { get; private set; }
        internal Cardinality Cardinality { get; private set; }

        #region Protected
        protected SymbolDef(NamespaceDef namespaceDef, XElement def, Cardinality defaultCardinality)
        {
            this.Type = null;
            this.namespaceDef = namespaceDef;
            this.Cardinality = defaultCardinality;
            this.Name = def.Attribute("name").Value;

            var idAttrib = def.Attribute("id");
            if (idAttrib == null)
            {
                idAttrib = def.Attribute("ID");
            }
            this.Id = Guid.Parse(idAttrib.Value);

            var cardAttrib = def.Attribute("cardinality");
            if (cardAttrib != null)
            {
                this.Cardinality = LookupCardinalityByName(cardAttrib.Value);
            }
        }

        protected static Localize LookupLocalizeByName(string name)
        {
            if (name.Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                return Localize.Yes;
            }
            else
            {
                return Localize.No;
            }
        }

        protected static Cardinality LookupCardinalityByName(string name)
        {
            if (name.Equals("single", StringComparison.OrdinalIgnoreCase))
            {
                return Cardinality.Single;
            }
            else if (name.Equals("multiple", StringComparison.OrdinalIgnoreCase))
            {
                return Cardinality.Multiple;
            }

            return Cardinality.Unknown;
        }
        #endregion
    }
}
